package cn.itsource.org.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 部门控制器
 */
@RestController
@RequestMapping("/dept")
@Api(tags = "部门管理", description = "部门操作接口")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询所有部门信息", notes = "查询所有部门信息，get请求")
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    /**
     * 查询所有
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "查询部门分页数据", notes = "查询数据")
    public PageList<Department> findPage(@RequestBody DepartmentQuery departmentQuery) {
        //查询分页数据
        return departmentService.findPage(departmentQuery);
    }

    /**
     * 哈哈哈哈
     * 新增或修改的操作
     * 根据id来区分新增和修改
     * 怎么区分请求使用的请求方式  这里是post
     */
    @PostMapping
    public AjaxResult saveOrUpdate(@RequestBody @Valid Department department) {

        //对所有的入参进行一个有效判断
        /*Optional.ofNullable(department.getName()).orElseThrow(() -> new RuntimeException("部门名称不能为空"));

        if (StringUtils.isBlank(department.getSn())){
            throw  new RuntimeException("部门编号不能为空");
        }
*/
        try {
            if (department.getId() == null) {
//没有id就是新增
                departmentService.save(department);
            } else {
//修改
                departmentService.update(department);
            }
            //返回成功对象
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败对象
            return AjaxResult.createError("操作异常,向日葵！");
        }
    }


    /**
     * 删除操作
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {

        try {
            //删除
            departmentService.delete(id);
            //返回成功对象
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败对象
            return AjaxResult.createError("操作异常,向日葵！");
        }
    }

    /**
     * 批量删除操作
     */
    @PostMapping("/batchDelete")
    public AjaxResult delete(@RequestBody DepartmentQuery departmentQuery) {

        try {
            //批量删除操作
            departmentService.batchDelete(departmentQuery);
            //返回成功对象
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败对象
            return AjaxResult.createError("操作异常,向日葵！");
        }
    }


    /**
     * 获取部门无限层的数据
     */
    @GetMapping("/tree")
    public AjaxResult tree() {

        try {
            //删除
            List<Department> tree = departmentService.getTree();
            //返回成功对象
            return AjaxResult.createSuccess(tree);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败对象
            return AjaxResult.createError("操作异常,向日葵！");
        }
    }

}
